<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * cli 模式下的服务接口 控制器
 */

/**
 * Description of socket_server
 *
 * @author Administrator
 */
class Sms_service extends CLI_Controller{
    public function __construct() {
        parent::__construct();
        $this->load->library('socket_server');
        $this->load->config('sms',true);
    }
    //短信发送服务
    public function service()
    {
        CLI_FUN::echo_system("短信接口开始执行 \n");
        $this->socket_server->set_port($this->config->item('sms_service_port','sms'));
        $this->socket_server->set_ip($this->config->item('sms_service_ip','sms'));
        try
        {
            $this->socket_server->init();
            while ($row = $this->socket_server->send())
            {
                if(false === $row)
                {
                    break;
                }
                else if(is_string($row) && strlen($row) > 0)
                {
                    $sms_data = json_decode($row);
                    if(is_object($sms_data))
                    {
                        $this->load->library('sms');
                        $type = $this->sms->send(urldecode($sms_data->sms),$sms_data->number);
                        if($type == 0)
                        {
                            $this->log_model->add('sms:post:true',"发送一条短信成功:\n号码:{$sms_data->sms}\n内容:{$sms_data->number}",'system');
                            CLI_FUN::echo_system("发送一条短信成功:\n号码:{$sms_data->number}\n内容:{$sms_data->sms}");
                        }
                        else
                        {
                            $this->log_model->add('sms:post:false',"发送一条短信失败:\n号码:{$sms_data->sms}\n内容:{$sms_data->number}",'system');
                            CLI_FUN::echo_system("发送一条短信失败:\n错误类型为:{$this->sms->error($type)}\n号码:{$sms_data->sms}\n内容:{$sms_data->number}");
                        }
                    }
                    else
                    {
                            CLI_FUN::echo_system("参数错误");
                    }
                }
            }
            CLI_FUN::echo_system("短信接口停止");
        }catch (Exception $e) {
            CLI_FUN::echo_system($e->getMessage());
        }
    }
    
    /*
     * 接收一条发送的请求 并发送给服务器
     */
    public function post()
    {
        $number = $this->input->post('number');
        $sms = $this->input->post('sms');
        $key = $this->input->post('key');
        if($key !== $this->config->item('sms_key','sms'))
        {
            CLI_FUN::echo_system(json_encode(array('type'=>false,'msg'=>'非法的提交')));
            return;
        }
        if(preg_match('"/13[0-9]{9}|15[0|1|2|3|5|6|7|8|9]\d{8}|18[0|5|6|7|8|9]\d{8}/"', $number) && $sms)
        {
            $this->socket_client->set_address($this->CI->config->item('sms_service_port','sms'));
            $this->socket_client->set_port($this->CI->config->item('sms_service_ip','sms'));
            $this->socket_client->set_socket_message(json_encode(array('sms'=>$sms,'number'=>$number)));
            $data = $this->socket_client->init();
            CLI_FUN::echo_system(json_encode(array('type'=>true,'msg'=>'服务器返回:'.$data)));
            return;
        }
        else
        {
            CLI_FUN::echo_system(json_encode(array('type'=>false,'msg'=>'提交的内容有误')));
            return;
        }
    }
    
    public function ceshi()
    {
        $this->load->library('socket_client');
        $this->socket_client->set_address($this->config->item('sms_service_ip','sms'));
        $this->socket_client->set_port($this->config->item('sms_service_port','sms'));
        $this->socket_client->set_socket_message(json_encode(array('sms'=>'发个短信试试','number'=>'13511255367')));
        $data = $this->socket_client->init();
        CLI_FUN::echo_system(json_encode(array('type'=>true,'msg'=>'服务器返回:'.$data)));
    }
}